package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.RegionQuery;
import cn.wolfcode.wolf2w.repository.RegionRepository;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.util.AssertUtil;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
//@Transactional
public class RegionServiceImpl implements IRegionService {

    //操作MongoDB数据库
    @Autowired
    private RegionRepository regionRepository;

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public void save(Region region) {
        regionRepository.save(region);
    }

    @Override
    public void delete(String id) {
        regionRepository.deleteById(id);
    }

    @Override
    public void update(Region region) {
        regionRepository.save(region);
    }


    @Override
    public Region findByOne(String id) {
        return regionRepository.findById(id).orElse(null);
    }

    @Override
    public void saveOrUpdate(Region region) {
        AssertUtil.hasObject("格式不正确",region);
        if (region.getId()!=null) {
            this.update(region);
        } else {
            this.save(region);
        }
    }

    /**
     * 高查
     * @param qo
     * @return
     */
    @Override
    public Object query(RegionQuery qo) {
        //创建查询对象
        Query query = new Query();
        //数据总条数
        long count = mongoTemplate.count(query, Region.class);
        if (count==0) {
            //如果没有数据,返回空Page对象
            return Page.empty();
        }
        count = mongoTemplate.count(query, Region.class);
        PageRequest pageRequest = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.Direction.ASC, "_id");
        //添加页请求对象
        query.with(pageRequest);
        //查询数据库
        List<Region> list = mongoTemplate.find(query, Region.class);
        return new PageImpl<>(list,pageRequest,count);
    }

    /**
     * 热门设置
     * @param id 修改的区域
     * @param hot 修改的值
     * @return
     */
    @Override
    public JsonResult changeHotValue(String id, String hot) {
        //创建查询对象
        Query query = new Query();
        //添加查询条件条件
        query.addCriteria(Criteria.where("_id").is(id));
        //创建修改对象
//        Update update = new Update();
        //设置修改的字段
        Update update = Update.update("ishot", hot);
//        update.set("ishot",hot);
        //执行修改操作(将匹配的结果都进行修改操作)
        mongoTemplate.updateMulti(query,update,Region.class);
        return JsonResult.success();
    }

    @Override
    public List<Region> findAll() {
        return regionRepository.findAll();
    }
}
